﻿using System;
using System.Drawing;
using System.Linq;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace XYZ_Inventory
{
    public partial class RawMaterialOrderDeliveries : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
                LoadGridView();

            tblCheckIn.Visible = false;
        }

        private void LoadGridView()
        {
            XYZDBContext dbContext = new XYZDBContext();
            GridView1.DataSource =
                dbContext.RawMaterialOrders.Where(x => x.Status == "Pending")
                    .Join(dbContext.RawMaterialInventories, x => x.RawMaterialID, y => y.RawMaterialID,
                        (a, b) => new
                        {
                            a.ID,
                            a.OrderDate,
                            b.RawMaterialName,
                            a.Quantity,
                            a.SupplierID
                        });
            GridView1.DataBind();
        }

        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GridView1.EditIndex = e.NewEditIndex;
            LoadGridView();
        }

        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            XYZDBContext dbContext = new XYZDBContext();
            int orderID = Convert.ToInt32(e.Keys[0]);
            RawMaterialOrder order = dbContext.RawMaterialOrders.Single(x => x.ID == orderID);
            GridViewRow row = GridView1.Rows[e.RowIndex];
            TextBox txtQuantity = (TextBox) row.FindControl("txtQuantity");
            int qty = Convert.ToInt32(txtQuantity.Text);
            order.Quantity = qty;
            dbContext.SaveChanges();
            GridView1.EditIndex = -1;
            LoadGridView();
        }

        protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            GridView1.EditIndex = -1;
            e.Cancel = true;
            LoadGridView();
        }

        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (GridView1.SelectedDataKey != null) txtOrderId.Text = GridView1.SelectedDataKey[0].ToString();
            btnDelivered.Enabled = true;
        }

        protected void btnDelivered_Click(object sender, EventArgs e)
        {
            XYZDBContext dbContext = new XYZDBContext();
            // New raw material order deliver
            RawMaterialDelivery orderDelivery = new RawMaterialDelivery();
            int orderId = Convert.ToInt32(txtOrderId.Text);
            orderDelivery.ID = orderId;
            if (!string.IsNullOrEmpty(txtUOQty.Text))
                orderDelivery.UnderOrderedItemQty = Convert.ToInt32(txtUOQty.Text);

            // Get order details
            RawMaterialOrder order = dbContext.RawMaterialOrders.Single(x => x.ID == orderId);
            int materialId = order.RawMaterialID;
            int qty = order.Quantity;

            // Get product from inventory
            RawMaterialInventory product = dbContext.RawMaterialInventories.Single(x => x.RawMaterialID == materialId);

            if (rdOverCount.Checked)
            {
                if (!string.IsNullOrEmpty(txtMistakeCount.Text))
                    orderDelivery.OverOrderedItemQty = Convert.ToInt32(txtMistakeCount.Text);
                orderDelivery.UnderOrderedItemQty = 0;
            }
            else
            {
                if (!string.IsNullOrEmpty(txtMistakeCount.Text))
                    orderDelivery.UnderOrderedItemQty = Convert.ToInt32(txtMistakeCount.Text);
                orderDelivery.OverOrderedItemQty = 0;
                qty -= Convert.ToInt32(txtMistakeCount.Text);
            }

            if (!string.IsNullOrEmpty(txtDamageCount.Text))
                orderDelivery.DamagedItemQty = Convert.ToInt32(txtDamageCount.Text);

            // Order updates
            order.DeliveryDate = DateTime.Now;
            order.Status = "Delivered";
            dbContext.RawMaterialDeliveries.AddObject(orderDelivery);

            // Product stock updates
            if (!string.IsNullOrEmpty(txtDamageCount.Text))
                qty -= Convert.ToInt32(txtDamageCount.Text);
            product.Stock += qty;

            dbContext.SaveChanges();
            LoadGridView();

            btnDelivered.Enabled = false;
            txtOrderId.Text = "";
            txtDamageCount.Text = "";
            txtMistakeCount.Text = "";
            txtUOQty.Text = "";
            lblStatus.Text = "Order updated as delivered";
            lblStatus.ForeColor = Color.Green;

            GridView1.SelectedIndex = -1;
        }

        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView1.PageIndex = e.NewPageIndex;
            LoadGridView();
        }

        protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
        {
            tblCheckIn.Visible = true;
        }
    }
}